//
//  NetworkManager.m
//  RACDemo
//
//  Created by XFB on 2018/8/8.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "NetworkManager.h"
#import <CommonCrypto/CommonDigest.h>


static NSString *const salt = @"gn1002015";

@implementation NetworkManager

//+ (void)getDeviceInfo
//{
//    UIDevice *device = [[UIDevice alloc] init];
//    NSString *name = device.name;
//    NSString *model = device.name;
//    NSString *type = device.localizedModel;
//    NSString *systemName = device.systemName;
//    NSString *systemVersion = device.systemVersion;
//    NSLog(@"%@", systemVersion);
//}

+ (NSDictionary *)paramWithList
{
    NSDictionary *params = @{
                             @"city" : @"中国",
                             @"cityId" : @0,
                             @"condition" : @"35,33,32,35,34",
                             @"teacherSeach" : @"1000,1000,1000",
                             };
    /// 获取版本号
    NSDictionary *dict = [NSBundle mainBundle].infoDictionary;
    NSString *version = [dict objectForKey:@"CFBundleShortVersionString"];
    
    /// 获取当前时间
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
    NSTimeInterval dateInterval = [date timeIntervalSince1970];
    int currentTime = (int) dateInterval;

    /// MD5加密处理
    NSString *keyMD5 = [self md5ForParams:params time:currentTime];
    
    NSDictionary *parameter = @{
                                @"u" : @"i",
                                @"v" : version,
                                @"time" : @(currentTime),
                                @"params" : params,
                                @"key" : keyMD5,
                                };
    NSLog(@"%@", parameter);
    
    return parameter;
}

+ (NSString *)md5ForParams:(NSDictionary *)params time:(int)time
{
    // NSJSONReadingAllowFragments 的意思
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONReadingAllowFragments error:nil];
    NSString *jsonParserString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    /// 加盐处理
    NSString *myString = [NSString stringWithFormat:@"%@%d%@", jsonParserString, time, salt];
    
    NSString *keyMD5 = [self getMd5_32Bit_String:myString];
    NSString *keymd5 = [self getMd5_32Bit_String:keyMD5];
    
    return keymd5;
}

+ (NSString *)getMd5_32Bit_String:(NSString *)srcString
{
    const char *cStr = [srcString UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    
    return result;
}

@end
